﻿// ***********************************************************************
// Assembly         : FG_Controls
// Created          : 05-17-2022
//
// ***********************************************************************
// <copyright file="UCStepTimeLine.cs">
//     Copyright by XJH

// ***********************************************************************
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;

namespace HZH_Controls.Controls
{
	/// <summary>
	/// Class UCStepTimeLine.
	/// Implements the <see cref="System.Windows.Forms.UserControl" />
	/// </summary>
	/// <seealso cref="System.Windows.Forms.UserControl" />
	[DefaultEvent("IndexChecked")]
	public partial class UCStepTimeLine : UserControl
	{
		#region VAR
		/// <summary>
		/// 步骤是否只读
		/// </summary>
		[Description("步骤是否只读"), Category("自定义")]
		public bool StepReadOnly
		{
			get; set;
		}
		[Description("中间缺失步骤是否自动完成"), Category("自定义")]
		public bool IsStepAutoCompleted
		{
			get; set;
		}
		/// <summary>
		/// Occurs when [index checked].
		/// </summary>
		[Description("步骤更改事件"), Category("自定义")]
		public event EventHandler IndexChecked;

		/// <summary>
		/// The m step back color
		/// </summary>
		private Color m_stepBackColor = Color.FromArgb(189, 189, 189);
		/// <summary>
		/// 步骤背景色
		/// </summary>
		/// <value>The color of the step back.</value>
		[Description("步骤背景色"), Category("自定义")]
		public Color StepBackColor
		{
			get { return m_stepBackColor; }
			set
			{
				m_stepBackColor = value;
				Refresh();
			}
		}

		/// <summary>
		/// The m step fore color
		/// </summary>
		private Color m_stepForeColor = Color.FromArgb(255, 77, 59);
		/// <summary>
		/// 步骤前景色
		/// </summary>
		/// <value>The color of the step fore.</value>
		[Description("步骤前景色"), Category("自定义")]
		public Color StepForeColor
		{
			get { return m_stepForeColor; }
			set
			{
				m_stepForeColor = value;
				Refresh();
			}
		}

		/// <summary>
		/// The m step font color
		/// </summary>
		private Color m_stepFontColor = Color.White;
		/// <summary>
		/// 步骤文字颜色
		/// </summary>
		/// <value>The color of the step font.</value>
		[Description("步骤文字景色"), Category("自定义")]
		public Color StepFontColor
		{
			get { return m_stepFontColor; }
			set
			{
				m_stepFontColor = value;
				Refresh();
			}
		}

		/// <summary>
		/// The title font
		/// </summary>
		private Font titleFont = new Font("微软雅黑", 11f);

		/// <summary>
		/// Gets or sets the title font.
		/// </summary>
		/// <value>The title font.</value>
		[Description("标题字体"), Category("自定义")]
		public Font TitleFont
		{
			get { return titleFont; }
			set
			{
				titleFont = value;
				Refresh();
			}
		}

		/// <summary>
		/// The title forcolor
		/// </summary>
		private Color titleForcolor = Color.Black;

		/// <summary>
		/// Gets or sets the title forcolor.
		/// </summary>
		/// <value>The title forcolor.</value>
		[Description("标题颜色"), Category("自定义")]
		public Color TitleForcolor
		{
			get { return titleForcolor; }
			set
			{
				titleForcolor = value;
				Refresh();
			}
		}

		/// <summary>
		/// The details font
		/// </summary>
		private Font detailsFont = new Font("微软雅黑", 10);

		/// <summary>
		/// Gets or sets the details font.
		/// </summary>
		/// <value>The details font.</value>
		[Description("详情字体"), Category("自定义")]
		public Font DetailsFont
		{
			get { return detailsFont; }
			set
			{
				detailsFont = value;
				Refresh();
			}
		}

		/// <summary>
		/// The details forcolor
		/// </summary>
		private Color detailsForcolor = Color.Gray;

		/// <summary>
		/// Gets or sets the details forcolor.
		/// </summary>
		/// <value>The details forcolor.</value>
		[Description("详情颜色"), Category("自定义")]
		public Color DetailsForcolor
		{
			get { return detailsForcolor; }
			set
			{
				detailsForcolor = value;
				Refresh();
			}
		}

		/// <summary>
		/// The m step width
		/// </summary>
		private int m_stepWidth = 35;
		/// <summary>
		/// 步骤宽度
		/// </summary>
		/// <value>The width of the step.</value>
		[Description("步骤宽度景色"), Category("自定义")]
		public int StepWidth
		{
			get { return m_stepWidth; }
			set
			{
				m_stepWidth = value;
				Refresh();
			}
		}

		/// <summary>
		/// The items
		/// </summary>
		StepTimeLineItem[] m_steps;

		/// <summary>
		/// Gets or sets the items.
		/// </summary>
		/// <value>The items.</value>
		[Description("项列表"), Category("自定义")]
		public StepTimeLineItem[] Steps
		{
			get { return m_steps; }
			set
			{
				if (value == null || value.Length < 1)
					return;

				var lst = new List<StepTimeLineItem>(value);
				lst.RemoveAll(t => string.IsNullOrWhiteSpace(t.Title));
				for (var i = 0; i < lst.Count; i++)
				{
					if (lst[i].IsCompleted) m_stepIndex = i + 1;
				}
				m_steps = lst.ToArray();
				Refresh();
			}
		}
		/// <summary>
		/// The m step index
		/// </summary>
		private int m_stepIndex = 0;

		/// <summary>
		/// Gets or sets the index of the step.
		/// </summary>
		/// <value>The index of the step.</value>
		[Description("步骤位置"), Category("自定义")]
		public int StepIndex
		{
			get { return m_stepIndex; }
			set
			{
				if (value > Steps?.Length)
					return;
				m_stepIndex = value;
				Refresh();
				if (IndexChecked != null)
				{
					IndexChecked(this, null);
				}
			}
		}

		/// <summary>
		/// The m line width
		/// </summary>
		private int m_lineWidth = 2;

		/// <summary>
		/// Gets or sets the width of the line.
		/// </summary>
		/// <value>The width of the line.</value>
		[Description("连接线宽度,最小2"), Category("自定义")]
		public int LineWidth
		{
			get { return m_lineWidth; }
			set
			{
				if (value < 2)
					return;
				m_lineWidth = value;
				Refresh();
			}
		}

		/// <summary>
		/// The m img completed
		/// </summary>
		private Image m_imgCompleted = null;
		/// <summary>
		/// Gets or sets the img completed.
		/// </summary>
		/// <value>The img completed.</value>
		[Description("已完成步骤图片，当不为空时，已完成步骤将不再显示数字,建议24*24大小"), Category("自定义")]
		public Image ImgCompleted
		{
			get { return m_imgCompleted; }
			set
			{
				m_imgCompleted = value;
				Refresh();
			}
		}
		/// <summary>
		/// The m img not completed
		/// </summary>
		private Image m_imgMissingCompleted = null;
		/// <summary>
		/// Gets or sets the img not completed.
		/// </summary>
		/// <value>The img not completed.</value>
		[Description("步骤缺失图片,建议24*24大小"), Category("自定义")]
		public Image ImgMissingCompleted
		{
			get { return m_imgMissingCompleted; }
			set
			{
				m_imgMissingCompleted = value;
				Refresh();
			}
		}
		public bool m_showCompletedImg = false;
		/// <summary>
		/// 显示完成步骤图片
		/// </summary>
		[Description("显示完成步骤图片√"), Category("自定义")]
		public bool ShowCompletedImg
		{
			get { return m_showCompletedImg; }
			set
			{
				m_showCompletedImg = value;
				Refresh();
			}
		}

		/// <summary>
		/// The m LST cache rect
		/// </summary>
		List<Rectangle> m_lstCacheRect = new List<Rectangle>();
		#endregion

		/// <summary>
		/// Initializes a new instance of the <see cref="UCStepTimeLine" /> class.
		/// </summary>
		public UCStepTimeLine()
		{
			System.ComponentModel.ComponentResourceManager resources =
				new System.ComponentModel.ComponentResourceManager(typeof(UCStepTimeLine));
			InitializeComponent();
			this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
			this.SetStyle(ControlStyles.DoubleBuffer, true);
			this.SetStyle(ControlStyles.ResizeRedraw, true);
			this.SetStyle(ControlStyles.Selectable, true);
			this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
			this.SetStyle(ControlStyles.UserPaint, true);
			this.MouseDown += UCStep_MouseDown;

			this.ImgCompleted = ((System.Drawing.Image)(resources.GetObject("ucStepTitle.ImgCompleted")));
		}

		/// <summary>
		/// Handles the MouseDown event of the UCStepTimeLine control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="MouseEventArgs" /> instance containing the event data.</param>
		void UCStep_MouseDown(object sender, MouseEventArgs e)
		{
			if (StepReadOnly) return;
			var index = m_lstCacheRect.FindIndex(p => p.Contains(e.Location));
			if (index >= 0)
			{
				StepIndex = index + 1;
			}
		}
		/// <summary>
		/// 引发 <see cref="E:System.Windows.Forms.Control.Paint" /> 事件。
		/// </summary>
		/// <param name="e">包含事件数据的 <see cref="T:System.Windows.Forms.PaintEventArgs" />。</param>
		protected override void OnPaint(PaintEventArgs e)
		{
			base.OnPaint(e);
			var g = e.Graphics;
			g.SetGDIHigh();

			if (m_steps != null && m_steps.Length > 0)
			{
				System.Drawing.SizeF sizeFirst = g.GetTextSizeF(m_steps[0].Title, this.Font);
				int y = (this.Height - m_stepWidth - 10 - (int)sizeFirst.Height) / 2 + 10;
				if (y < 0)
					y = 0;

				int intTxtY = y + m_stepWidth;
				int intLeft = 0;//左边空间
				if (sizeFirst.Width > m_stepWidth)
				{
					intLeft = (int)(sizeFirst.Width - m_stepWidth) / 2;
				}
				intLeft += 20;

				int intRight = 0;//右边空间
				System.Drawing.SizeF sizeEnd = g.GetTextSizeF(m_steps[m_steps.Length - 1].Title, this.Font);
				if (sizeEnd.Width > m_stepWidth)
				{
					intRight = (int)(sizeEnd.Width - m_stepWidth) / 2 + 1;
				}
				intRight += 40;

				int intSplitWidth = 20;
				if (m_steps.Length > 1)
				{
					intSplitWidth = (this.Width - m_steps.Length - (m_steps.Length * m_stepWidth) - intRight - intLeft) / (m_steps.Length - 1);
				}

				m_lstCacheRect = new List<Rectangle>();
				for (int i = 0; i < m_steps.Length; i++)
				{
					#region 画圆，横线
					Rectangle rectEllipse = new Rectangle(new Point(intLeft + i * (m_stepWidth + intSplitWidth), y), new Size(m_stepWidth, m_stepWidth));
					m_lstCacheRect.Add(rectEllipse);
					g.FillEllipse(new SolidBrush(m_stepBackColor), rectEllipse);
					//-----------------------------------------------------------------------------------------------------------------
					Image m_img = null;
					var isCompleted = m_stepIndex > i && (IsStepAutoCompleted || m_steps[i].IsCompleted);
					if (ShowCompletedImg && isCompleted)
					{
						//当前步骤已完成或后面步骤是自动跟随完成
						m_img = m_steps[i].ImgCompleted != null ? m_steps[i].ImgCompleted : m_imgCompleted;
					}
					else if (ShowCompletedImg && (m_stepIndex > i && !IsStepAutoCompleted && !m_steps[i].IsCompleted))
					{
						m_img = m_steps[i].ImgMissingCompleted != null ? m_steps[i].ImgMissingCompleted : m_imgMissingCompleted;
					}
					//-----------------------------------------------------------------------------------------------------------------
					if (isCompleted)
					{
						g.FillEllipse(new SolidBrush(m_stepForeColor), new Rectangle(new Point(intLeft + i * (m_stepWidth + intSplitWidth) + 2, y + 2), new Size(m_stepWidth - 4, m_stepWidth - 4)));
					}
					if (m_img != null)
					{
						g.DrawImage(m_img, new Rectangle(new Point((intLeft + i * (m_stepWidth + intSplitWidth) + (m_stepWidth - 24) / 2)
																		, y + (m_stepWidth - 24) / 2), new Size(24, 24))
										, 0, 0, m_img.Width, m_img.Height, GraphicsUnit.Pixel, null);
					}
					else
					{
						var _numSize = g.GetTextSizeF((i + 1).ToString(), this.Font);
						g.DrawString((i + 1).ToString(), Font, new SolidBrush(m_stepFontColor)
							, new Point(intLeft + i * (m_stepWidth + intSplitWidth) + (m_stepWidth - (int)_numSize.Width) / 2 - 1, y + (m_stepWidth - (int)_numSize.Height) / 2 + 1));
					}
					#endregion

					#region 画Title
					if (!string.IsNullOrWhiteSpace(m_steps[i].Title))
					{
						var sizeTxt = g.GetTextSizeF(m_steps[i].Title, this.titleFont);
						g.DrawString(m_steps[i].Title, titleFont, new SolidBrush(m_stepIndex > i ? titleForcolor : m_stepBackColor)
							, new Point(intLeft + i * (m_stepWidth + intSplitWidth) + (m_stepWidth - (int)sizeTxt.Width) / 2
									  , intTxtY - (int)sizeTxt.Height - m_stepWidth - 10));
					}
					#endregion
					#region 画Details
					if (!string.IsNullOrWhiteSpace(m_steps[i].Details))
					{
						var dy = y + m_stepWidth + 5;
						var rect = new Rectangle(new Point(intLeft + i * (m_stepWidth + intSplitWidth) - m_stepWidth / 2, dy)
											   , new Size(m_stepWidth + intSplitWidth - m_stepWidth / 2, this.Height - dy));
						g.DrawStringWrap(detailsFont, m_steps[i].Details, rect, m_stepIndex > i ? detailsForcolor : m_stepBackColor);
					}
					#endregion

					#region 画步骤连接线
					var ptY = y + m_stepWidth / 2;
					var m_stepColor = isCompleted ? m_stepForeColor : m_stepBackColor;

					if (i != m_steps.Length - 1)
					{
						var ptBegX = intLeft + i * (m_stepWidth + intSplitWidth) + m_stepWidth - 3;
						var ptMidX = intLeft + i * (m_stepWidth + intSplitWidth) + m_stepWidth + intSplitWidth / 2;
						g.DrawLine(new Pen(m_stepColor, m_lineWidth), new Point(ptBegX, ptY), new Point(ptMidX, ptY));
					}
					if (i > 0)
					{
						var ptPreMidX = intLeft + (i - 1) * (m_stepWidth + intSplitWidth) + m_stepWidth + intSplitWidth / 2 - 1;
						var ptPreBegX = intLeft + i * (m_stepWidth + intSplitWidth) + 3;
						g.DrawLine(new Pen(m_stepColor, m_lineWidth), new Point(ptPreMidX, ptY), new Point(ptPreBegX, ptY));
					}
					#endregion
				}
			}

		}

	}
}
